package com.wjd.common.valid.groups;

import javax.validation.ConstraintValidator;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {

    private Set<Integer> values;

    @Override
    public void initialize(ListValue constraintAnnotation) {
        this.values = Arrays.stream(constraintAnnotation.vals())
                .boxed()
                .collect(Collectors.toSet());
    }

    @Override
    public boolean isValid(Integer value, javax.validation.ConstraintValidatorContext context) {
        return values.contains(value);
    }

}
